package com.example.administrator.college02;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.administrator.college02.fragment.FriendFragment;
import com.example.administrator.college02.fragment.LifeFragment;
import com.example.administrator.college02.fragment.MessageFragment;
import com.example.administrator.college02.fragment.PersonFragment;
import com.example.administrator.college02.fragment.SchoolFragment;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends BaseActivity {

    private SchoolFragment schoolFragment = null;
    private LifeFragment lifeFragment = null;
    private FriendFragment friendFragment = null;
    private MessageFragment messageFragment = null;
    private PersonFragment personFragment = null;
    private FragmentManager fm = null;
    @BindView(R.id.rg_bottom) RadioGroup rgBottomBar;
    @BindView(R.id.rb_bottom_bar_item1) RadioButton rbItem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        fm = getSupportFragmentManager();
        initViews();
    }

    private void initViews () {
        rgBottomBar.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                FragmentTransaction ft = fm.beginTransaction();
                hideAllFragments(ft);
                switch (i) {
                    case R.id.rb_bottom_bar_item1:
                        if (null == schoolFragment) {
                            schoolFragment = new SchoolFragment();
                            ft.add(R.id.fl_main, schoolFragment);
                        } else {
                            ft.show(schoolFragment);
                        }
                        break;
                    case R.id.rb_bottom_bar_item2:
                        if (null == lifeFragment) {
                            lifeFragment = new LifeFragment();
                            ft.add(R.id.fl_main, lifeFragment);
                        } else {
                            ft.show(lifeFragment);
                        }
                        break;
                    case R.id.rb_bottom_bar_item3:
                        if (null == friendFragment) {
                            friendFragment = new FriendFragment();
                            ft.add(R.id.fl_main, friendFragment);
                        } else {
                            ft.show(friendFragment);
                        }
                        break;
                    case R.id.rb_bottom_bar_item4:
                        if (null == messageFragment) {
                            messageFragment = new MessageFragment();
                            ft.add(R.id.fl_main, messageFragment);
                        } else {
                            ft.show(messageFragment);
                        }
                        break;
                    case R.id.rb_bottom_bar_item5:
                        if (null == personFragment) {
                            personFragment = new PersonFragment();
                            ft.add(R.id.fl_main, personFragment);
                        } else {
                            ft.show(personFragment);
                        }
                        break;
                }
                ft.commit();
            }
        });
        rbItem.setChecked(true);
    }
    private void hideAllFragments (FragmentTransaction ft) {
        if (null != schoolFragment){ft.hide(schoolFragment);}
        if (null != lifeFragment){ft.hide(lifeFragment);}
        if (null != friendFragment){ft.hide(friendFragment);}
        if (null != messageFragment){ft.hide(messageFragment);}
        if (null != personFragment){ft.hide(personFragment);}
    }
//    long firstTime = 0 ;
//
//    @Override
//    public boolean onKeyUp(int keyCode, KeyEvent event) {
//        if (keyCode == KeyEvent.KEYCODE_BACK) {
//            long secondTime = System.currentTimeMillis();
//            if (secondTime - firstTime > 1600) {//如果两次按键时间间隔大于1600毫秒，则不退出
//                Toast.makeText(MainActivity.this, "再按一次退出程序...",
//                        Toast.LENGTH_SHORT).show();
//                firstTime = secondTime;//更新firstTime
//                return true;
//            } else {
//                System.exit(0);//否则退出程序
//            }
//        }
//        return super.onKeyUp(keyCode, event);
//    }

}
